Login/ Register With Google Open Id

Course- PHP Tutorial >

This article will guide you to crate login/ register with Google.

Lets create a basic registration from with First Name, Last Name, Email and Password. Here we can give an option to the user to register/ login to our system with their Google  Id. The process is like this: If the user click on "Login with Google" button then it will ask Google user name and password. If the user is using the system first tie then it will redirect to registration page, but it will display first name, last name and email fields. It will not show password field. Once the user click on submit, our system will save the data in the server with the reference number sent by Google. Next time when the user come and click on "Login with Google" our system will check the reference number in our database, if it matches then it will sent the user directly to My Account page.

Please note in this process we will not save password in the database. You can follow the same steps for login with yahoo.

You can download the attached file and get the basic html form and other files used in this project. This project required an external file called openid.php, which you will get in the attached zip file.

Step1:

Include your database connection file and openid.php to you login page.

<?php
 include('connection.php');
 require_once 'google/openid.php';  $googleid = new LightOpenID("a2zebhelp.com");
 $googleid->identity = 'https://www.google.com/accounts/o8/id';
 $googleid->required = array(
   'namePerson/first',
   'namePerson/last',
   'contact/email',);
$googleid->returnUrl = 'http://a2zwebhelp.com/login-google.php';
?>


Now Add the following code to "Login with Google" button.

<a href="<?php echo $googleid->authUrl() ?>">Login with Google</a>


If the user enters the correct info then Google page will redirect back to or login page with required information's.

if ($googleid->mode == 'cancel') {
echo "User has canceled authentication !";
} elseif($googleid->validate()) {
$data = $googleid->getAttributes();
$email = $data['contact/email'];
$firstname = $data['namePerson/first'];
$lastname = $data['namePerson/last'];

$Identity = explode("=",$googleid->identity);
$userid = $Identity[1];
$loginwith = 'Google';

$sql = mysql_query("select userid from register where passcode='".$userid."'");
$numrow = mysql_num_rows($sql);
if($numrow > 0){
header('Location:myaccount.php');
exit();
}
}


If the user registered before then our system will redirect the user to my account page otherwise it will ask the user to register for the first time.

if($_POST['register']){

$fname = mysql_real_escape_string($_POST['fname']);
$lname = mysql_real_escape_string($_POST['lname']);
$email = mysql_real_escape_string($_POST['email']); 
$passcode = mysql_real_escape_string($_POST['passcode']);
$loginwith = mysql_real_escape_string($_POST['loginwith']);
$status = 'active';

$mysql = mysql_query("insert into register set fastname = '".$fname."', 
lastname = '".$lname."',
email = '".$email."',
passcode = '".$passcode."',
loginwith = '".$loginwith."',
status = '".$status."'");

header('Location:myaccount.php');
}


The MySql table used in this project

CREATE TABLE IF NOT EXISTS `register` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`fastname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`passcode` varchar(100) NOT NULL,
`loginwith` varchar(20) NOT NULL,
`status` varchar(10) NOT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `userid` (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;